<template>
  <el-collapse-item name="product">
    <template slot="title">
      <PartitionTitle title="产品信息"></PartitionTitle>
    </template>

    <el-table :data="productList" tooltip-effect="dark" style="width: 100%" border>
      <el-table-column label="序号" type="index" width="80" align="center">
        <template slot-scope="scope">
          <span>{{
            utils.tableSerialNumber(scope, pager.pageNum, pager.pageSize)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" prop="erpItemCode"></el-table-column>
      <el-table-column label="产品名称" prop="erpItemName"></el-table-column>
      <el-table-column label="WIP数量" prop="erpPackageNumber">
        <template slot-scope="scope" >
          <el-input-number v-model="scope.row.erpPackageNumber" size="mini" v-if='mode == "add"'></el-input-number>
          <span v-else>{{scope.row.erpPackageNumber}}</span>
        </template>
      </el-table-column>

      <el-table-column label="实际产量" prop="actualQuantity" v-if='mode == "update" || mode == "history"'>
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.actualQuantity" size="mini" :min="0" :disabled='mode == "history"' @change="changNum"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="erpItemSpec"></el-table-column>
    </el-table>
  </el-collapse-item>
</template>
<script>
  import {refershOrderQtys} from "@/api/workOrder/confirmClose";
export default {
  props:{
    mode:{
      type:String,
      default:""
    },
    productList:{
      type:Array,
      default:()=>[]
    },
    orderDetail:{
      type:Object,
      default:()=>{}
    }
  },
  data() {
    return {
      promise: null,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10
      }
    };
  },

  methods:{
    changNum(currentValue, oldValue){
      if(Number(currentValue) < 0){
        this.$set(this.productList[0],"actualQuantity",0);
        this.$forceUpdate()
      }
      if(String(currentValue).indexOf(".") > -1){//带小数点
        this.$set(this.productList[0],"actualQuantity",Math.floor(currentValue));
        this.$forceUpdate()
      }
      const parmas = {
        id: this.orderDetail.id,
        collectedQuantity: this.orderDetail.collectedQuantity,
        actualQuantity: this.productList[0].actualQuantity,
        orderMaterialDtos:this.orderDetail?.orderMaterialDtos
      }
      this.promise = refershOrderQtys(parmas).then(res => {
        if (res.code == 200 && res.data) {
          const tableList = res?.data?.orderMaterialDtos;
          this.msgSuccess("数据更新成功")
          this.$emit("refershOrderQtys",tableList,parmas.actualQuantity)
          this.$set(this.productList[0],"isUpdated",true);
        } else {
          this.msgError(res.msg || "数据更新失败")
        }
      }).catch(() => {
        this.msgError("数据更新失败")
      })
    },
    async checkChangeNum() {
      if (this.productList[0].isUpdated != true) {
        await this.promise
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.block-title {
  margin-bottom: 0;
}
</style>
